package com.cloudlinkscm.loms.services.base.notify.dto.email.builder;

import com.cloudlinkscm.loms.services.base.notify.dto.email.Cc;
import com.cloudlinkscm.loms.services.base.notify.dto.email.Ccls;
import com.cloudlinkscm.loms.services.base.notify.dto.email.EmailMessageDto;
import com.cloudlinkscm.loms.services.base.notify.dto.email.builder.component.EmailMessageDtoBodyBuilderDecorator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * {@link EmailMessageDto}构建器
 *
 * @author : tac
 * @date : 2017/7/12
 */
public class EmailMessageDtoBuilderConcrete implements EmailMessageDtoBuilder {
    private String senderMail;
    private String senderName;
    private String recipientName;
    private String recipientMail;
    private String subject;
    private Ccls ccls;

    public EmailMessageDtoBuilderConcrete(String senderName, String recipientName, String recipientMail, String subject) {
        this.senderName = senderName;
        this.recipientName = recipientName;
        this.recipientMail = recipientMail;
        this.subject = subject;
        this.ccls = new Ccls();
    }

    @Override
    public EmailMessageDto build() {
        EmailMessageDto dto = new EmailMessageDto(senderName, recipientName, recipientMail, subject);
        dto.setSenderMail(senderMail);
        try {
            dto.setRecipientCcls(new ObjectMapper().writeValueAsString(ccls));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            throw new RuntimeException("ccls序列化失败");
        }
        return dto;
    }

    public EmailMessageDtoBuilderConcrete sender(String senderMail){
        this.senderMail = senderMail;
        return this;
    }

    public EmailMessageDtoBuilderConcrete cc(String recipient, String mail) {
        Cc cc = new Cc(recipient, mail);
        this.ccls.add(cc);
        return this;
    }

    public EmailMessageDtoBodyBuilderDecorator next(){
        return new EmailMessageDtoBodyBuilderDecorator(this);
    }
}
